Java检测死锁之ThreadMXBean

public static void main(String[] args) {
        ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
        // 只能检测 synachrozied 同步代码块 的死锁
        // long[] deadlockedThreadIds = mbean.findMonitorDeadlockedThreads();

        // 可以检测 juc下的 Lock造成的死锁和 synachrozied代码块的死锁
        long[] deadlockedThreadIds = mbean.findDeadlockedThreads();

        if (deadlockedThreadIds != null) {
            ThreadInfo[] threadInfos = mbean.getThreadInfo(deadlockedThreadIds);

            for (ThreadInfo ti : threadInfos) {
                System.out.println(ti);
            }
        }
    }

 

posted @ 2018-03-16 16:08  liron  阅读(694)  评论(0编辑  收藏  举报